package cn.szv5.register.client;

import java.util.UUID;

/**
 * 应用模块名称<p> 在服务上被创建和启动，负责跟register-server进行通信<p> Copyright: Copyright (C) 2022 , Inc. All rights reserved. <p> Company:
 * 阿飞工作室<p>
 *
 * @author Carl
 * @since 2022/2/7 18:41
 */
public class RegisterClient {

    /**
     * 服务实例ID
     */
    private String serviceInstanceId;

    public static final String SERVICE_NAME = "inventory-service";
    public static final String IP = "192.168.31.207";
    public static final String HOSTNAME = "inventory01";
    public static final int PORT = 9000;
    /**
     * http通信组件
     */
    private HttpSender httpSender;

    private static final Long CHECK_ALIVE_INTERVAL = 30 * 1000L;

    /**
     * 是否在运行
     */
    private volatile boolean isRunning;

    /**
     * 心跳线程
     */
    private HeartbeatWorker heartbeatWorker;

    /**
     * 客户端缓存的服务注册表
     */
    private CachedServiceRegistry registry;

    public RegisterClient() {
        httpSender = new HttpSender();
        serviceInstanceId = UUID.randomUUID().toString().replace("-", "");
        heartbeatWorker = new HeartbeatWorker();
        isRunning = true;
        registry = new CachedServiceRegistry(this, httpSender);
    }

    /**
     * 启动register-client组件
     */
    public void start() {
        try {
            // 服务注册
            RegisterWorker registerWorker = new RegisterWorker();
            registerWorker.start();
            registerWorker.join();

            // 启动心跳线程,定时发送心跳信息
            heartbeatWorker.start();

            // 初始化客户端缓存的服务注册表
            registry.initialize();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    /**
     * 停止register-client组件
     */
    public void shutdown() {
        isRunning = false;
        heartbeatWorker.interrupt();
        registry.destroy();
        httpSender.cancel(SERVICE_NAME, serviceInstanceId);
    }

    /**
     * 心跳线程
     */
    private class HeartbeatWorker extends Thread {
        @Override
        public void run() {
            HeartbeatRequest heartbeatRequest = new HeartbeatRequest();
            heartbeatRequest.setServiceName(SERVICE_NAME);
            heartbeatRequest.setServiceInstanceId(serviceInstanceId);

            HeartbeatResponse heartbeatResponse = null;

            while (isRunning) {
                heartbeatResponse = httpSender.heartbeat(heartbeatRequest);
                System.out.println("心跳的结果为：" + heartbeatResponse.getStatus());
                try {
                    Thread.sleep(CHECK_ALIVE_INTERVAL);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    /**
     * 注册线程
     */
    private class RegisterWorker extends Thread {

        @Override
        public void run() {
            RegisterRequest registerRequest = new RegisterRequest();
            registerRequest.setServiceName(SERVICE_NAME);
            registerRequest.setIp(IP);
            registerRequest.setHostname(HOSTNAME);
            registerRequest.setPort(PORT);
            registerRequest.setServiceInstanceId(UUID.randomUUID().toString().replace("-", ""));

            RegisterResponse registerResponse = httpSender.register(registerRequest);
            System.out.println("服务注册的结果是：" + registerResponse.getStatus());
        }
    }

    public boolean isRunning() {
        return isRunning;
    }
}
